package cn.flying.system.service.cm.impl;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import cn.flying.base.common.vo.PageRequest;
import cn.flying.base.common.vo.PageResponse;
import cn.flying.base.common.vo.XmSelect;
import cn.flying.system.common.enums.SeqTypeEnum;
import cn.flying.system.domain.cm.CmRole;
import cn.flying.system.domain.cm.CmRoleExample;
import cn.flying.system.domain.cm.CmRoleResource;
import cn.flying.system.domain.cm.CmRoleResourceExample;
import cn.flying.system.domain.cm.CmUserRole;
import cn.flying.system.domain.cm.CmUserRoleExample;
import cn.flying.system.mapper.cm.CmRoleMapper;
import cn.flying.system.mapper.cm.CmRoleResourceMapper;
import cn.flying.system.mapper.cm.CmUserRoleMapper;
import cn.flying.system.service.cm.CmRoleService;
import cn.flying.system.service.cm.CmSeqService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @description: 角色接口实现类
 * @author: lvyong
 * @date: 2022年06月04日 21:09
 * @version: 1.0
 */
@Service
public class CmRoleServiceImpl implements CmRoleService {

    @Resource
    private CmRoleMapper cmRoleMapper;
    @Resource
    private CmUserRoleMapper cmUserRoleMapper;
    @Resource
    private CmRoleResourceMapper cmRoleResourceMapper;
    @Resource
    private CmSeqService cmSeqService;

    /**
     * 查询列表-分页
     *
     * @param pageNum
     * @param pageSize
     * @param cmRole
     * @return
     */
    @Override
    public PageResponse<CmRole> getRolePageList(PageRequest<CmRole> pageRequest) {
        Integer pageNum = pageRequest.getPageNum();
        Integer pageSize = pageRequest.getPageSize();
        CmRole cmRole = pageRequest.getParam();
        CmRoleExample example = new CmRoleExample();
        CmRoleExample.Criteria criteria = example.createCriteria();
        if (StringUtils.hasText(cmRole.getRoleId())) {
            criteria.andRoleIdEqualTo(cmRole.getRoleId());
        }
        if (StringUtils.hasText(cmRole.getRoleName())) {
            criteria.andRoleNameLike("%" + cmRole.getRoleName() + "%");
        }
        PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        Page<CmRole> page = (Page<CmRole>) cmRoleMapper.selectByExample(example);
        return new PageResponse(page.getTotal(), pageNum, page.getResult());
    }

    /**
     * 根据主键ID查询信息
     *
     * @param id
     * @return
     */
    @Override
    public CmRole getRoleById(Long id) {
        return cmRoleMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取角色下拉框数据 xm-select
     *
     * @return
     */
    @Override
    public List<XmSelect> getRoleList() {
        CmRoleExample example = new CmRoleExample();
        List<CmRole> roles = cmRoleMapper.selectByExample(example);
        List<XmSelect> list = new ArrayList<>();
        XmSelect xmSelect;
        for (CmRole role : roles) {
            xmSelect = new XmSelect();
            xmSelect.setName(role.getRoleName());
            xmSelect.setValue(role.getRoleId());
            xmSelect.setSelected(false);
            xmSelect.setDisabled(false);

            list.add(xmSelect);
        }
        return list;
    }

    /**
     * 查询角色对应的资源集合
     *
     * @param roleId
     * @return
     */
    @Override
    public List<CmRoleResource> getRoleResourceList(String roleId) {
        CmRoleResourceExample example = new CmRoleResourceExample();
        CmRoleResourceExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        return cmRoleResourceMapper.selectByExample(example);
    }

    /**
     * 查询角色对应的资源集合
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<CmRoleResource> getRoleResourceListByRoles(List<String> roleIds) {
        CmRoleResourceExample example = new CmRoleResourceExample();
        CmRoleResourceExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdIn(roleIds);
        return cmRoleResourceMapper.selectByExample(example);
    }

    /**
     * 查询用户的角色信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<CmUserRole> getUserRoleList(String userId) {
        CmUserRoleExample example = new CmUserRoleExample();
        CmUserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<CmUserRole> roles = cmUserRoleMapper.selectByExample(example);
        return roles;
    }

    /**
     * 保存新增信息
     *
     * @param role
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertRole(CmRole role) {
        String seq = cmSeqService.getSeq(SeqTypeEnum.ROLE.name());
        role.setRoleId(seq);
        cmRoleMapper.insertSelective(role);
    }

    /**
     * 保存修改信息
     *
     * @param role
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateRoleById(CmRole role) {
        cmRoleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 根据主键ID删除
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteRoleById(Long id) {
        cmRoleMapper.deleteByPrimaryKey(id);
    }

    /**
     * 保存角色资源关联
     *
     * @param permissions
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertRoleResources(List<CmRoleResource> permissions) {
        if (CollectionUtils.isEmpty(permissions)) {
            return;
        }
        String roleId = permissions.get(0).getRoleId();
        CmRoleResourceExample example = new CmRoleResourceExample();
        CmRoleResourceExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        cmRoleResourceMapper.deleteByExample(example);

        for (CmRoleResource roleResource : permissions) {
            cmRoleResourceMapper.insert(roleResource);
        }
    }
}
